<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目ID" prop="projectId">
        <el-input
          v-model="queryParams.projectId"
          placeholder="请输入项目ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品ID" prop="productId">
        <el-input
          v-model="queryParams.productId"
          placeholder="请输入产品ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商ID" prop="supplierId">
        <el-input
          v-model="queryParams.supplierId"
          placeholder="请输入供应商ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联供应商报价单ID" prop="supplierQuoteId">
        <el-input
          v-model="queryParams.supplierQuoteId"
          placeholder="请输入关联供应商报价单ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报价日期" prop="quoteDate">
        <el-date-picker clearable
          v-model="queryParams.quoteDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择报价日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="绘图材质" prop="drawingMaterial">
        <el-input
          v-model="queryParams.drawingMaterial"
          placeholder="请输入绘图材质"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="材质" prop="material">
        <el-input
          v-model="queryParams.material"
          placeholder="请输入材质"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="阻燃等级" prop="inflamingRetardingLevel">
        <el-input
          v-model="queryParams.inflamingRetardingLevel"
          placeholder="请输入阻燃等级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['scm:scmEstimateQuotes:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['scm:scmEstimateQuotes:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['scm:scmEstimateQuotes:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['scm:scmEstimateQuotes:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scmEstimateQuotesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="id" />
      <el-table-column label="项目ID" align="center" prop="projectId" />
      <el-table-column label="产品ID" align="center" prop="productId" />
      <el-table-column label="供应商ID" align="center" prop="supplierId" />
      <el-table-column label="关联供应商报价单ID" align="center" prop="supplierQuoteId" />
      <el-table-column label="供应商名称" align="center" prop="supplierName" />
      <el-table-column label="报价日期" align="center" prop="quoteDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.quoteDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="绘图材质" align="center" prop="drawingMaterial" />
      <el-table-column label="材质" align="center" prop="material" />
      <el-table-column label="阻燃等级" align="center" prop="inflamingRetardingLevel" />
      <el-table-column label="是否含税" align="center" prop="taxIncluded">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.taxIncluded"/>
        </template>
      </el-table-column>
      <el-table-column label="是否含运费" align="center" prop="freightIncluded">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.freightIncluded"/>
        </template>
      </el-table-column>
      <el-table-column label="一次性数量" align="center" prop="disposableQuantity" />
      <el-table-column label="一次性单位" align="center" prop="disposableUnit" />
      <el-table-column label="一次性单位单价" align="center" prop="disposableUnitPrice" />
      <el-table-column label="试做数量" align="center" prop="tryLot" />
      <el-table-column label="试做单位" align="center" prop="tryUnit" />
      <el-table-column label="试做单位单价" align="center" prop="tryUnitPrice" />
      <el-table-column label="试做总价" align="center" prop="tryTotalPrice" />
      <el-table-column label="量产数量" align="center" prop="massLot" />
      <el-table-column label="量产单位" align="center" prop="massUnit" />
      <el-table-column label="量产单位单价" align="center" prop="massUnitPrice" />
      <el-table-column label="模具费" align="center" prop="moldCost" />
      <el-table-column label="工装费" align="center" prop="jigCost" />
      <el-table-column label="模具试做天数" align="center" prop="moldTryingDeliveryDays" />
      <el-table-column label="模具使用寿命(次)" align="center" prop="moldLife" />
      <el-table-column label="一次性交付天数" align="center" prop="disposableDeliveryDays" />
      <el-table-column label="试做交付天数" align="center" prop="tryDeliveryDays" />
      <el-table-column label="量产交付天数" align="center" prop="massDeliveryDays" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="scm_select_satus" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="状态原因" align="center" prop="statusReason" />
      <el-table-column label="报价文件名字" align="center" prop="quoteFileName" />
      <el-table-column label="报价文件地址" align="center" prop="quoteFileUrl" />
      <el-table-column label="沟通内容" align="center" prop="communicationContent" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['scm:scmEstimateQuotes:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['scm:scmEstimateQuotes:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户报价单对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="scmEstimateQuotesRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目ID" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入项目ID" />
        </el-form-item>
        <el-form-item label="产品ID" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入产品ID" />
        </el-form-item>
        <el-form-item label="供应商ID" prop="supplierId">
          <el-input v-model="form.supplierId" placeholder="请输入供应商ID" />
        </el-form-item>
        <el-form-item label="关联供应商报价单ID" prop="supplierQuoteId">
          <el-input v-model="form.supplierQuoteId" placeholder="请输入关联供应商报价单ID" />
        </el-form-item>
        <el-form-item label="供应商名称" prop="supplierName">
          <el-input v-model="form.supplierName" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="报价日期" prop="quoteDate">
          <el-date-picker clearable
            v-model="form.quoteDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择报价日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="绘图材质" prop="drawingMaterial">
          <el-input v-model="form.drawingMaterial" placeholder="请输入绘图材质" />
        </el-form-item>
        <el-form-item label="材质" prop="material">
          <el-input v-model="form.material" placeholder="请输入材质" />
        </el-form-item>
        <el-form-item label="阻燃等级" prop="inflamingRetardingLevel">
          <el-input v-model="form.inflamingRetardingLevel" placeholder="请输入阻燃等级" />
        </el-form-item>
        <el-form-item label="是否含税" prop="taxIncluded">
          <el-select v-model="form.taxIncluded" placeholder="请选择是否含税">
            <el-option
              v-for="dict in sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否含运费" prop="freightIncluded">
          <el-select v-model="form.freightIncluded" placeholder="请选择是否含运费">
            <el-option
              v-for="dict in sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="一次性数量" prop="disposableQuantity">
          <el-input v-model="form.disposableQuantity" placeholder="请输入一次性数量" />
        </el-form-item>
        <el-form-item label="一次性单位" prop="disposableUnit">
          <el-input v-model="form.disposableUnit" placeholder="请输入一次性单位" />
        </el-form-item>
        <el-form-item label="一次性单位单价" prop="disposableUnitPrice">
          <el-input v-model="form.disposableUnitPrice" placeholder="请输入一次性单位单价" />
        </el-form-item>
        <el-form-item label="试做数量" prop="tryLot">
          <el-input v-model="form.tryLot" placeholder="请输入试做数量" />
        </el-form-item>
        <el-form-item label="试做单位" prop="tryUnit">
          <el-input v-model="form.tryUnit" placeholder="请输入试做单位" />
        </el-form-item>
        <el-form-item label="试做单位单价" prop="tryUnitPrice">
          <el-input v-model="form.tryUnitPrice" placeholder="请输入试做单位单价" />
        </el-form-item>
        <el-form-item label="试做总价" prop="tryTotalPrice">
          <el-input v-model="form.tryTotalPrice" placeholder="请输入试做总价" />
        </el-form-item>
        <el-form-item label="量产数量" prop="massLot">
          <el-input v-model="form.massLot" placeholder="请输入量产数量" />
        </el-form-item>
        <el-form-item label="量产单位" prop="massUnit">
          <el-input v-model="form.massUnit" placeholder="请输入量产单位" />
        </el-form-item>
        <el-form-item label="量产单位单价" prop="massUnitPrice">
          <el-input v-model="form.massUnitPrice" placeholder="请输入量产单位单价" />
        </el-form-item>
        <el-form-item label="模具费" prop="moldCost">
          <el-input v-model="form.moldCost" placeholder="请输入模具费" />
        </el-form-item>
        <el-form-item label="工装费" prop="jigCost">
          <el-input v-model="form.jigCost" placeholder="请输入工装费" />
        </el-form-item>
        <el-form-item label="模具试做天数" prop="moldTryingDeliveryDays">
          <el-input v-model="form.moldTryingDeliveryDays" placeholder="请输入模具试做天数" />
        </el-form-item>
        <el-form-item label="模具使用寿命(次)" prop="moldLife">
          <el-input v-model="form.moldLife" placeholder="请输入模具使用寿命(次)" />
        </el-form-item>
        <el-form-item label="一次性交付天数" prop="disposableDeliveryDays">
          <el-input v-model="form.disposableDeliveryDays" placeholder="请输入一次性交付天数" />
        </el-form-item>
        <el-form-item label="试做交付天数" prop="tryDeliveryDays">
          <el-input v-model="form.tryDeliveryDays" placeholder="请输入试做交付天数" />
        </el-form-item>
        <el-form-item label="量产交付天数" prop="massDeliveryDays">
          <el-input v-model="form.massDeliveryDays" placeholder="请输入量产交付天数" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in scm_select_satus"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态原因" prop="statusReason">
          <el-input v-model="form.statusReason" placeholder="请输入状态原因" />
        </el-form-item>
        <el-form-item label="报价文件名字" prop="quoteFileName">
          <el-input v-model="form.quoteFileName" placeholder="请输入报价文件名字" />
        </el-form-item>
        <el-form-item label="报价文件地址" prop="quoteFileUrl">
          <el-input v-model="form.quoteFileUrl" placeholder="请输入报价文件地址" />
        </el-form-item>
        <el-form-item label="沟通内容">
          <editor v-model="form.communicationContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-divider content-position="center">估算报价单额外明细信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddScmEstimateQuoteItems">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteScmEstimateQuoteItems">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="scmEstimateQuoteItemsList" :row-class-name="rowScmEstimateQuoteItemsIndex" @selection-change="handleScmEstimateQuoteItemsSelectionChange" ref="scmEstimateQuoteItems">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="量产数量" prop="massLot" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.massLot" placeholder="请输入量产数量" />
            </template>
          </el-table-column>
          <el-table-column label="量产单位" prop="massUnit" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.massUnit" placeholder="请输入量产单位" />
            </template>
          </el-table-column>
          <el-table-column label="量产单位单价" prop="massUnitPrice" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.massUnitPrice" placeholder="请输入量产单位单价" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ScmEstimateQuotes">
import { listScmEstimateQuotes, getScmEstimateQuotes, delScmEstimateQuotes, addScmEstimateQuotes, updateScmEstimateQuotes } from "@/api/scm/scmEstimateQuotes";

const { proxy } = getCurrentInstance();
const { scm_select_satus, sys_yes_no } = proxy.useDict('scm_select_satus', 'sys_yes_no');

const scmEstimateQuotesList = ref([]);
const scmEstimateQuoteItemsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedScmEstimateQuoteItems = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectId: null,
    productId: null,
    supplierId: null,
    supplierQuoteId: null,
    supplierName: null,
    quoteDate: null,
    drawingMaterial: null,
    material: null,
    inflamingRetardingLevel: null,
  },
  rules: {
    projectId: [
      { required: true, message: "项目ID不能为空", trigger: "blur" }
    ],
    productId: [
      { required: true, message: "产品ID不能为空", trigger: "blur" }
    ],
    supplierName: [
      { required: true, message: "供应商名称不能为空", trigger: "blur" }
    ],
    quoteDate: [
      { required: true, message: "报价日期不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询客户报价单列表 */
function getList() {
  loading.value = true;
  listScmEstimateQuotes(queryParams.value).then(response => {
    scmEstimateQuotesList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    projectId: null,
    productId: null,
    supplierId: null,
    supplierQuoteId: null,
    supplierName: null,
    quoteDate: null,
    drawingMaterial: null,
    material: null,
    inflamingRetardingLevel: null,
    taxIncluded: null,
    freightIncluded: null,
    disposableQuantity: null,
    disposableUnit: null,
    disposableUnitPrice: null,
    tryLot: null,
    tryUnit: null,
    tryUnitPrice: null,
    tryTotalPrice: null,
    massLot: null,
    massUnit: null,
    massUnitPrice: null,
    moldCost: null,
    jigCost: null,
    moldTryingDeliveryDays: null,
    moldLife: null,
    disposableDeliveryDays: null,
    tryDeliveryDays: null,
    massDeliveryDays: null,
    status: null,
    statusReason: null,
    quoteFileName: null,
    quoteFileUrl: null,
    communicationContent: null,
    remarks: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  scmEstimateQuoteItemsList.value = [];
  proxy.resetForm("scmEstimateQuotesRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加客户报价单";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getScmEstimateQuotes(_id).then(response => {
    form.value = response.data;
    scmEstimateQuoteItemsList.value = response.data.scmEstimateQuoteItemsList;
    open.value = true;
    title.value = "修改客户报价单";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["scmEstimateQuotesRef"].validate(valid => {
    if (valid) {
      form.value.scmEstimateQuoteItemsList = scmEstimateQuoteItemsList.value;
      if (form.value.id != null) {
        updateScmEstimateQuotes(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addScmEstimateQuotes(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除客户报价单编号为"' + _ids + '"的数据项？').then(function() {
    return delScmEstimateQuotes(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 估算报价单额外明细序号 */
function rowScmEstimateQuoteItemsIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 估算报价单额外明细添加按钮操作 */
function handleAddScmEstimateQuoteItems() {
  let obj = {};
  obj.massLot = "";
  obj.massUnit = "";
  obj.massUnitPrice = "";
  scmEstimateQuoteItemsList.value.push(obj);
}

/** 估算报价单额外明细删除按钮操作 */
function handleDeleteScmEstimateQuoteItems() {
  if (checkedScmEstimateQuoteItems.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的估算报价单额外明细数据");
  } else {
    const scmEstimateQuoteItemss = scmEstimateQuoteItemsList.value;
    const checkedScmEstimateQuoteItemss = checkedScmEstimateQuoteItems.value;
    scmEstimateQuoteItemsList.value = scmEstimateQuoteItemss.filter(function(item) {
      return checkedScmEstimateQuoteItemss.indexOf(item.index) == -1
    });
  }
}

/** 复选框选中数据 */
function handleScmEstimateQuoteItemsSelectionChange(selection) {
  checkedScmEstimateQuoteItems.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('scm/scmEstimateQuotes/export', {
    ...queryParams.value
  }, `scmEstimateQuotes_${new Date().getTime()}.xlsx`)
}

getList();
</script>
